$(function (){
    //获取用户信息
    let user = sessionStorage.getItem('user');
    user = JSON.parse(user);

    function jumpUser2(){
        // 设置登录完成后的跳转URL
        const currentUrl = window.location.href;
        sessionStorage.setItem('redirectUrl', currentUrl);
        window.location.href = '../userInfologin.html';
    }

    //获取商品信息
    let good = sessionStorage.getItem('good');
    good = JSON.parse(good);

    $('.preview-wrap').find('img').prop('src','http://project-x.cn:9000/youwushoppingmall/product/'+good.pictureUrl);
    $('.biaoti').find('b').text(good.pictureName)
    $('.two').find('.price').text(good.picturePrice)
    $('.jiage').find('.mum').text(good.pictureNumber)
    $('.two').find('.qvan').text(good.discount)

    $('#jrgwc').on('click',function (){
        if (user != null){
            $.post('shoppingCart/join_shoppingCart', {pictureId:user.userId,shoppingCommodityId:good.commodityId},function (){
                let total = $('#total span').text();
                total ++;
                $('#total span').text(total);
            });
        }else {
            jumpUser2();
        }
    })
    $('#sc').on('click',function (){
        if ($(this).find('img').prop('src') === 'http://localhost:8080/image/foreground/xingxing.png'){
            $(this).find('img').prop('src','../image/foreground/星星.png')
            return;
        }
        $(this).find('img').prop('src','../image/foreground/xingxing.png')
    })

    let vm2 = new Vue({
        el:'#like',
        data:{
            goods:[],
        },
        methods:{
            goodDetail(id){
                $.post('product/get_product_by_cid',{cid:id},function (res){
                    sessionStorage.setItem("good",JSON.stringify(res.data)) ;
                    window.location.href = '../shop.html'
                })
            }
        },
        beforeCreate:function (){
            $.post('product/list_product_bySubtypeId',{sId:good.commoditySubtypeId,cId:good.commodityId},function (res){
                vm2.goods = res.data;
                vm2.goods = vm2.goods.slice(0, 2);
            })
        }
    })
})